﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using SmallSpider.Class;


namespace SmallSpider
{
    public partial class CatchWebSites : Form
    {
        SpiderThread _SpiderThread = new SpiderThread();

        private int ThreadCount
        {
            get
            {
                return _SpiderThread.ThreadCount;
            }
            set
            {
                _SpiderThread.ThreadCount = value;
            }
        }

        private int StopThreadCount = 0;

        public CatchWebSites()
        {
            InitializeComponent();
            splitContainer1.Height = this.Height - (toolStrip1.Height + 12 + statusStrip1.Height + 12);
            splitContainer1.Width = this.Width - 1;
            _SpiderThread.ReceivedData += _SpiderThread_ReceivedData;
            _SpiderThread.ThreadFinish += _SpiderThread_ThreadFinish;
        }

        private void Main_SizeChanged(object sender, EventArgs e)
        {
            splitContainer1.Height = this.Height - (toolStrip1.Height + 12 + statusStrip1.Height + 12);
            splitContainer1.Width = this.Width - 1;
            if (tabControl1.TabPages["threadInfo"] != null)
            {
                if (tabControl1.TabPages["threadInfo"].Controls.Count > 0)
                {
                    for (int i = 0; i < ThreadCount; i++)
                    {
                        tabControl1.TabPages["threadInfo"].Controls[i].Width =
                            tabControl1.TabPages["threadInfo"].Width / ThreadCount;
                    }
                }
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            InitTabControl();
            _SpiderThread.Start();
            this.btnStart.Enabled = false;
            btnStop.Enabled = true;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            btnStop.Enabled = false;
            SpiderThread.ThreadRun = false;
            stateCurrentInfo.Text = "正在关闭...";
        }

        private void _SpiderThread_ReceivedData(string strThreadName, string strOutPut)
        {
            CallbackHandler(OutputToTxtLog, strThreadName, strOutPut);
        }

        private void _SpiderThread_ThreadFinish(string strThreadName)
        {
            StopThreadCount++;
            if (StopThreadCount == ThreadCount)
            {
                ThreadAllStopHandler(EnableStart);
            }
        }

        #region << 通过委托来修改主线程控件 >>

        #region << 输出获取记录 >>

        private delegate void delegateOutputToTxtLog(string strThreadName, string strText);

        private void CallbackHandler(delegateOutputToTxtLog myDelegate, string strThreadName, string strText)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(myDelegate, strThreadName, strText);
            }
            else
            {
                OutputToTxtLog(strThreadName, strText);
            }
        }

        private void OutputToTxtLog(string strThreadName, string strText)
        {
            ListView lvLog = tabControl1.TabPages["threadInfo"].Controls["lv" + strThreadName] as ListView;
            ListViewItem lviUrl = new ListViewItem(strText.Split('：'));
            lvLog.Items.Add(lviUrl);
            if (lvLog.Items.Count > 20)
            {
                lvLog.Items.Remove(lvLog.Items[0]);
            }
        }

        #endregion

        #region << 开启开始按钮 >>

        private delegate void delegateEnableStart();

        private void ThreadAllStopHandler(delegateEnableStart myDelegate)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(myDelegate);
            }
            else
            {
                EnableStart();
            }
        }

        private void EnableStart()
        {
            btnStop.Enabled = false;
            this.btnStart.Enabled = true;
            stateCurrentInfo.Text = string.Empty;
        }

        #endregion
        

        #endregion


        /// <summary>初始化选项卡控件</summary>
        private void InitTabControl()
        {
            if (tabControl1.TabPages.ContainsKey("threadInfo")) return;
            tabControl1.TabPages.Add("threadInfo", "线程信息");
            if (tabControl1.TabPages["threadInfo"].Controls.Count != ThreadCount)
            {
                for (int i = 0; i < ThreadCount; i++)
                {
                    ListView lvLog = new ListView();
                    lvLog.GridLines = true; //显示表格线
                    lvLog.View = View.Details;//显示表格细节 
                    lvLog.Scrollable = true;//有滚动条
                    lvLog.HeaderStyle = ColumnHeaderStyle.Clickable;//对表头进行设置
                    lvLog.FullRowSelect = true;//是否可以选择行 
                    //添加表头
                    lvLog.Columns.Add("线程", 60);
                    lvLog.Columns.Add("网址", 200);
                    lvLog.Name = "lvThread" + i.ToString();
                    lvLog.Dock = DockStyle.Left;
                    lvLog.Width = tabControl1.TabPages["threadInfo"].Width / ThreadCount;
                    tabControl1.TabPages["threadInfo"].Controls.Add(lvLog);
                }
            }
            else
            {
                for (int i = 0; i < tabControl1.TabPages["threadInfo"].Controls.Count; i++)
                {
                    if (tabControl1.TabPages["threadInfo"].Controls[i] is ListView)
                    {
                        (tabControl1.TabPages["threadInfo"].Controls[i] as ListView).Items.Clear();
                    }
                }
            }
        }
    }
}
